#include "stdio.h"
#include "stdlib.h"
#define LT(a, b) (return (a) < (b) ? 1 : 0) 

void data_swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

void heap_adjust(int a[], int pos, int length)
{
    int cur = pos;
    while(cur >= 0)
    {
        int child = cur;
        for(; 2 * child + 2 < length; child++)
        {
            if (LT(a[2 * child + 1], a[2 * child + 2])) 
            {
                 if (LT(a[child], a[ 2 *child + 2]))
                     data_swap(&a[child], &a[2 * child + 2]);
            }
            else
            {
                 if (LT(a[child], a[ 2 *child + 1]))
                     data_swap(&a[child], &a[2 * child + 1]);
            }
        }
        cur--;
    }
}

void heap_sort(int arr[])
{
}

void heap_print(int arr[], int len)
{
    int i = 0;
    for (; i < len; i++)
        printf("%3d", arr[i]);
    printf("\n");
}

void main(int argc, char *argv[])
{
    int a[] = [10, 2, 3, 4, 6, 5, 7, 1];
    heap_adjust(a, 4, 8);
    heap_print(a, 8);
}
